<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system 
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : order_process_fixprice.class.php   FILE_PATH : E:\www\\trunk\classes\application\order_process_fixprice.class.php
 * ....交易流程操作类	交易类型:团购
 * 
 * array(
 * 	'retrun_type'=>'url',	返回类型  url 或者 showpage
 * 	'retrun_info'=>,	返回内容 url 或者页面名称
 * 	'url_type'=>'location',	url返回类型  location 直接跳转，redirect 经过跳转页面
 *  'url_message'=>'' 跳转页面显示信息
 * );
 * 
 * array(
 * 	'error'=>'1',//有错误
 * 	'error_msg'=>'',//错误提示
 * );
 * 
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Thu Feb 26 09:16:26 CST 2009
 */

defined('Base_Use_Sign') or die("this is invalid system!please contact <a href='http://www.shopnc.net'>www.shopnc.net</>.");

require_once('order.class.php');

class OrderProcessGroup extends ProductOrderClass {
	/**
	 * 订单内容
	 *
	 * @var obj
	 */
	var $order_info;
	/**
	 * 支付方式交易流水号
	 *
	 * @var obj
	 */
	var $alipay_id;
	/**
	 * 语言包内容
	 *
	 * @var obj
	 */
	var $_lang;
	
	/**
	 * 构造函数
	 * 
	 * @author workflow Develop Team     
	 * @param  $sp_code 订单编号
	 * @return array
	 */
	function OrderProcessFixprice($sp_code=''){
		$this->__construct($sp_code);
	}
	function __construct($sp_code=''){
		//取订单信息
		if ($sp_code != ''){
			$this->order_info = $this->getOneOrderBySpcode($sp_code);			
		}
		//初始化变量
		$this->_getConfiginfo();
	}
	
	/**
	 * 商品详情页面点击购买操作，信息输出到购买页面
	 * @author workflow Develop Team     
	 * @param  $param	传递参数
	 * @return array	输出到页面的信息
	 */
	function buy($param){
		return $param;
	}
	
	/**
	 * 提交购买信息，生成订单
	 * @author workflow Develop Team     
	 * @param  $param	购买参数
	 * @return bool	返回是否成功
	 */
	function order($param){
		//获得随机的唯一商品订单编码
		$product_order_last_id = $this->getProductOrderLastId();
		if("" == $product_order_last_id){
			$product_order_last_id = 1;
		}else{
			$product_order_last_id += 1;
		}
		$chars = array(
		"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
		"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
		"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
		"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
		"S", "T", "U", "V", "W", "X", "Y", "Z"
		);
		$random_string = Common::genRandomString($chars, 4);
		$param["txtSPcode"] = md5($product_order_last_id.$random_string);	
		
		//判断是否达到团购结束数量
		if(($param["have_sold"]+$param["txtBuyNum"]) < $param["group_mincount"]){//未达到团购结束数量
			$param["sp_state"] = 4;//团购中
		}else{//达到团购结束数量
			$change_state["sp_state"] = 0;
			$change_state["p_code"] = $param["txtPcode"];
			$change_state["sp_old_state"] = 4;
			//改变该商品团购订单状态为已购买
			$this->updateProductOrderByGroup($param["txtPcode"]);
			//已购买
			$param["sp_state"] = 0;
		}
		$param['sp_currency_category'] = $param[$param['txtPayment'].'_currency'];
		//生成订单
		$result = $this->saveProductOrder($param);
		if ($result === true){
			//更新商品信息
			$this->_updateProduct($param);
			//网站提醒操作
			$this->_remind($param['txtPcode'],'add_order');
			//判断是否是预付款支付，如果是，则生成预付款明细
			if ($param['txtPayment'] == 'predeposit'){
				//生成预付款第一份明细，第二份明细在买家确认收货
				require_once("predeposit.class.php");
				$obj_predeposit = new PredepositClass();
				//增加买家预付款明细
				$value_array = array();
				$value_array['predeposit_type'] = '2';//购买商品
				$value_array['predeposit_state'] = '1';
				$value_array['member_id'] = $_SESSION['s_login']['id'];
				$value_array['available_amount'] = '-'.($param['predeposit_price']*$param['txtBuyNum']+$param["txtTfFee"]);
				$value_array['to_member_id'] = $param['txtSellerId'];
				$value_array['sp_code'] = $param["txtSPcode"];
				$value_array['freeze_amount'] = $param['predeposit_price']*$param['txtBuyNum']+$param["txtTfFee"];
				$value_array['system_remark'] = $this->_lang['langOProductBuy'];
				$value_array['create_time'] = time();
				$value_array['update_time'] = time();
				$value_array['payment'] = 'predeposit';
				$obj_predeposit->addPredepositDetail($value_array);
				unset($value_array,$obj_predeposit);
				//对买家帐号进行操作
				require_once("member.class.php");
				$obj_member = new MemberClass();
				$value_array = array();
				$value_array['available_predeposit'] = '-'.$param['predeposit_price'];
				$value_array['freeze_predeposit'] = '+'.$param['predeposit_price'];
				$obj_member->modifyMember($value_array,$_SESSION['s_login']['id'],'predeposit');
				unset($value_array,$obj_member);
			}
			//预存款，线下
			if ($param['txtPayment'] == 'offline'){//如果是线下交易
				return array(
				'retrun_type'=>'url',
				'retrun_info'=>'member/own_order.php?action=group',
				'url_type'=>'redirect',
				'url_message'=>$this->_lang['langOBuyIsSucc'],
				);
			}else if ($param['txtPayment'] == 'predeposit'){
				return array(
				'retrun_type'=>'url',
				'retrun_info'=>'member/own_order.php?action=group',
				'url_type'=>'redirect',
				'url_message'=>$this->_lang['langOPredepositBuyIsSucc'],
				);
			}else {
				return array(
				'retrun_type'=>'url',
				'retrun_info'=>'member/own_order.php?action=group',
				'url_type'=>'redirect',
				'url_message'=>$this->_lang['langOBuyIsSucc'],
				);
			}
		}else {
			return array(
				'error'=>'1',//有错误
				'error_msg'=>$this->_lang['errPProductBuyFail'],//错误提示
			);
		}
	}
	
	/**
	 * 获取订单状态 
	 * 
	 * 说明：
	 * 取得订单状态，根据状态获取下一步操作的url(next_url)，文字等内容
	 * 返回信息 用来输出页面
	 * 用于列表等展示
	 * 
	 * array(
	 * 	'state_title'=>'',//状态名称
	 * 	'state_url'=>'',//下一步的url
	 * 	'state_type'=>'',//类别，卖家还是买家使用该URL	seller,buyer,all
	 * );
	 * 
	 * @author workflow Develop Team     
	 * @param  $sp_code 订单编号
	 * @return array
	 */
	function getOrderStateInfo($sp_code){
		if ($this->order_info != ''){
			$this->__construct($sp_code);
		}
		require_once("payment.class.php");
		$obj_payment = new PaymentClass();
		//根据状态
		switch ($this->order_info['sp_state']){
			case '0'://等待买家付款
				//判断支付方式跳转链接
				$payment_array = $obj_payment->returnPaymentPath($this->order_info['sp_pay_mechod'],$sp_code,2,$this->order_info['receive_code'],$this->order_info['sp_state']);
				break;
			case '1'://买家已支付
				//判断支付方式跳转链接
				$payment_array = $obj_payment->returnPaymentPath($this->order_info['sp_pay_mechod'],$sp_code,1,$this->order_info['receive_code'],$this->order_info['sp_state']);
				break;
			case '2'://卖家已发货
				//判断支付方式跳转链接
				$payment_array = $obj_payment->returnPaymentPath($this->order_info['sp_pay_mechod'],$sp_code,2,$this->order_info['receive_code'],$this->order_info['sp_state']);
				break;
			case '3'://买家已收货
				$payment_array = $obj_payment->returnPaymentPath($this->order_info['sp_pay_mechod'],$sp_code,2,$this->order_info['receive_code'],$this->order_info['sp_state']);
				break;
			case '6'://交易失败
				$payment_array = $obj_payment->returnPaymentPath($this->order_info['sp_pay_mechod'],$sp_code,2,$this->order_info['receive_code'],$this->order_info['sp_state']);
				break;
			case '7'://交易关闭
				$payment_array = $obj_payment->returnPaymentPath($this->order_info['sp_pay_mechod'],$sp_code,2,$this->order_info['receive_code'],$this->order_info['sp_state']);
				break;
		}
		unset($obj_payment);
		return $payment_array;
	}
	
	/**
	 * 改变订单状态
	 * 
	 * 说明:
	 * 团购的状态分为 已购买，已支付，已发货，已收货，团购中，团购失败
	 * 已购买状态 是生成订单的基本状态，不用改变，从已支付状态开始改变，已收货状态为最后的完成交易状态
	 * 
	 * @author workflow Develop Team     
	 * @param  $sp_code 订单编号	$to_state	改变到的状态
	 * @return bool
	 */
	function changeOrderState($sp_code,$to_state){
		switch ($to_state){
			case '0'://买家已购买
				//订单改变为该状态的时候需要调用的具体方法，返回需要使用的信息，比如链接，输出等信息
				
				break;
			case '1'://买家已支付
				$result = $this->_changeOrderStatePayed($sp_code);
				break;
			case '2'://卖家已发货
				$result = $this->_changeOrderStateSetInvoice($sp_code);
				break;
			case '3'://买家确认收货
				//成功完成购买操作
				CreditsClass::saveCreditsLog('succ_buy',$this->order_info['buyer_id']);
				$result = $this->_changeOrderStateReceived($sp_code);
				break;
		}
		return $result;
	}
	
	
	/**
	 * 私有方法 更新商品信息
	 * @author workflow Develop Team     
	 * @param  $param 商品编号等信息
	 * @return bool
	 */
	function _updateProduct($param){
		//更新商品售出数量,如达到商品发布数量商品下架
		if($param["allow_quantity"] == $param["txtBuyNum"]){
			$param["have_sold_all"] = 1;
		}else{
			$param["have_sold_all"] = 0;
		}
		$param["p_code"] = $param["txtPcode"];
		$param["decrease_num"] = $param["txtBuyNum"];//减少的数量
		//更新商品当期售出数量和累计售出数量
		require_once('product.class.php');
		$obj_product = new ProductClass();
		$result = $obj_product->updateProductSoldCount($param);
		unset($obj_product);
		return $result;
	}
	
	/**
	 * 私有方法 提醒操作
	 * 
	 * 说明：提醒种类	add_order	生成订单
	 * 
	 * @author workflow Develop Team     
	 * @param  $p_code 商品编号 $remind_type 提醒种类
	 * @return int/bool/object/array
	 */
	function _remind($p_code,$remind_type){
		/**
		 * 网站提醒操作
		 */
		require_once('remind.class.php');
		$obj_remind = new RemindClass();
		//取商品信息
		require_once('product.class.php');
		$obj_product = new ProductClass();
		$condition['p_code'] = $p_code;
		$array = $obj_product->getProductList($condition,$page,'member');
		$product_array = $array[0];
		//提醒类别
		switch ($remind_type){
			case 'add_order'://生成订单
			
				break;
			case 'set_invoice'://卖家发货，提醒买家
				require_once('member.class.php');
				$obj_member = new MemberClass();
				$condition['id'] = $this->order_info['buyer_id'];//买家ID
				$buyer_array = $obj_member->getMemberInfo($condition);
				$value_array = array ();
				$value_array ['username'] = $product_array['shopmaster'];
				$value_array ['product_name'] = $product_array['p_name'];
				$obj_remind->setMessageOrMail ( 'buyer_sale_to_succ', 'sale_to_succ', $value_array, $buyer_array ['shopmaster'], $this->_configinfo );
				unset($obj_member);
				break;
		}
		unset($obj_remind,$obj_product);
		return true;
	}
	
	/**
	 * 私有方法 修改订单状态 已支付
	 * @author workflow Develop Team     
	 * @param  $sp_code 订单编号 alipay_id交易流水号
	 * @return bool
	 */
	function _changeOrderStatePayed($sp_code){
		//更改订单状态,变成已发货
		$array = array();
		$array['spcode'] = $sp_code;
		$array['txtSPstate'] = 1;
		$array['pay_time'] = time ();
		$array['alipay_id'] = $this->alipay_id;
		$result = $this->updateProductOrderState($array);
		return $result;
	}
	
	/**
	 * 私有方法 修改订单状态 已发货
	 * @author workflow Develop Team     
	 * @param  $sp_code 订单编号
	 * @return bool
	 */
	function _changeOrderStateSetInvoice($sp_code){
		//卖家发货，提醒买家
		$this->_remind($this->order_info['p_code'],'set_invoice');
		//更改订单状态,变成已发货
		$array = array();
		$array['spcode'] = $sp_code;
		$array['txtSPstate'] = 2;
		$array['deliver_time'] = time ();
		$result = $this->updateProductOrderState($array);
		return $result;
	}
	
	/**
	 * 私有方法 修改订单状态 确认收货
	 * @author workflow Develop Team     
	 * @param  
	 * @return int/bool/object/array
	 */
	function _changeOrderStateReceived($sp_code){
		
		//预存款开启，并且支付方式为预存款
		if ($this->order_info['sp_pay_mechod'] == 'predeposit'){
			//确认收货，生成预付款明细，卖家为增加可用资金，买家为减少冻结资金
			require_once ("predeposit.class.php");
			$obj_predeposit = new PredepositClass ( );
			require_once("member.class.php");
			$obj_member = new MemberClass();
			//卖家
			$value_array = array ();
			$value_array ['predeposit_type'] = '3'; //卖出入账
			$value_array ['predeposit_state'] = '1';
			$value_array ['member_id'] = $this->order_info ['seller_id'];
			$value_array ['available_amount'] = $this->order_info ['total_price'];
			$value_array ['to_member_id'] = $this->order_info ['buyer_id'];
			$value_array ['sp_code'] = $this->order_info ["sp_code"];
			$value_array ['system_remark'] = $this->_lang['langOProductSold'];
			$value_array ['create_time'] = time ();
			$value_array ['update_time'] = time ();
			$value_array ['payment'] = 'predeposit';
			$obj_predeposit->addPredepositDetail ( $value_array );
			unset ( $value_array);
			//对卖家帐号进行操作
			$value_array = array ();
			$value_array ['available_predeposit'] = '+' . $this->order_info ['total_price'];
			$obj_member->modifyMember ( $value_array, $this->order_info ['seller_id'], 'predeposit' );
			unset ( $value_array );
			//买家
			$value_array = array ();
			$value_array ['predeposit_type'] = '2'; //购买商品
			$value_array ['predeposit_state'] = '1';
			$value_array ['member_id'] = $this->order_info ['buyer_id'];
			$value_array ['to_member_id'] = $this->order_info ['seller_id'];
			$value_array ['sp_code'] = $this->order_info ["sp_code"];
			$value_array ['freeze_amount'] = '-' . $this->order_info ['total_price'];
			$value_array ['system_remark'] = $this->_lang['langOProductBuy'];
			$value_array ['create_time'] = time ();
			$value_array ['update_time'] = time ();
			$value_array ['payment'] = 'predeposit';
			$obj_predeposit->addPredepositDetail ( $value_array );
			unset ( $value_array );
			//对买家帐号进行操作
			$value_array = array ();
			$value_array ['freeze_predeposit'] = '-' . $this->order_info ['total_price'];
			$obj_member->modifyMember ( $value_array, $this->order_info ['buyer_id'], 'predeposit' );
			unset ( $value_array );
			unset($obj_predeposit,$obj_member);
		}
		//更改订单状态,变成已发货
		$array = array();
		$array['spcode'] = $sp_code;
		$array['txtSPstate'] = 3;
		$array['deliver_time'] = time ();
		$array['receive_time'] = time ();
		if ($this->order_info['sp_pay_mechod'] == 'offline') {
			$array['pay_time'] = time ();
		}
		$result = $this->updateProductOrderState($array);
		return $result;
	}
	
}
?>